<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Chat Interface</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }

    .switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }

    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      transition: .4s;
    }

    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      transition: .4s;
    }

    input:checked + .slider {
      background-color: #2196F3;
    }

    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }

    input:checked + .slider:before {
      transform: translateX(26px);
    }

        .navbar {
            margin-bottom: 20px;
        }
        #chat-container {
            height: 70vh;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 15px;
            border-radius: 5px;
        }
        #chat-input {
            resize: none;
        }
        .message {
            margin-bottom: 10px;
            padding: 10px;
            border-radius: 5px;
        }
        .user-message {
            background-color: #e3f2fd;
            margin-left: 20%;
        }
        .ai-message {
            background-color: #f5f5f5;
            margin-right: 20%;
        }
        .typing-indicator {
            display: inline-block;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 5px;
        }
        .dot {
            display: inline-block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background-color: #666;
            margin: 0 2px;
            animation: bounce 1.4s infinite ease-in-out;
        }
        .dot:nth-child(2) {
            animation-delay: 0.2s;
        }
        .dot:nth-child(3) {
            animation-delay: 0.4s;
        }
        @keyframes bounce {
            0%, 80%, 100% { transform: translateY(0); }
            40% { transform: translateY(-8px); }
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">AI Chat</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('chat') }}">Chat</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('config') }}">MCP Configuration</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        <div style="display: flex;justify-content: space-between;align-items: center;">
            <h1>AI Chat</h1>
            <div style="display: flex;justify-content: space-between;align-items: center;">
                是否开启MCP服务
                <label class="switch">
                  <input type="checkbox">
                  <span class="slider"></span>
                </label>
            </div>
        </div>
        <div id="chat-container">
            <div id="chat-messages">
                <!-- Messages will appear here -->
            </div>
        </div>
        <div class="input-group mb-3">
            <textarea id="chat-input" class="form-control" placeholder="Type your message here..." rows="3"></textarea>
            <button id="send-button" class="btn btn-primary" type="button">Send</button>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        var switchButton = document.querySelector('.switch input');
        var open_mcp = false;

        switchButton.addEventListener('change', function() {
            if (this.checked) {
                // 执行开启状态的操作
                open_mcp = true;
            } else {
                // 执行关闭状态的操作
                open_mcp = false;
            }
        });

        document.addEventListener('DOMContentLoaded', function() {
            const chatInput = document.getElementById('chat-input');
            const sendButton = document.getElementById('send-button');
            const chatMessages = document.getElementById('chat-messages');
            

            function addMessage(role, content) {
                const messageDiv = document.createElement('div');
                messageDiv.className = `message ${role}-message`;
                messageDiv.textContent = content;
                chatMessages.appendChild(messageDiv);
                chatMessages.scrollTop = chatMessages.scrollHeight;
                return messageDiv;
            }

            function showTypingIndicator() {
                const indicator = document.createElement('div');
                indicator.className = 'message ai-message typing-indicator';
                indicator.id = 'typing-indicator';
                indicator.innerHTML = '<span class="dot"></span><span class="dot"></span><span class="dot"></span>';
                chatMessages.appendChild(indicator);
                chatMessages.scrollTop = chatMessages.scrollHeight;
                return indicator;
            }

            function hideTypingIndicator() {
                const indicator = document.getElementById('typing-indicator');
                if (indicator) {
                    indicator.remove();
                }
            }

            sendButton.addEventListener('click', async function() {
                const message = chatInput.value.trim();
                mcpConfig = JSON.parse(localStorage.getItem('mcpConfig'))
                if (!message) return;

                // Add user message to chat
                addMessage('user', message);
                chatInput.value = '';

                // Show typing indicator
                showTypingIndicator();

                // Get conversation history
                const messages = Array.from(document.querySelectorAll('.message')).map(el => {
                    return {
                        role: el.classList.contains('user-message') ? 'user' : 'assistant',
                        content: el.textContent
                    };
                }).filter(msg => !msg.content.includes('...')); // Exclude typing indicators
                // Call the API with streaming
                const response = await fetch('/api/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        messages: messages,
                        open_mcp: open_mcp,
                    })
                });

                if (!response.ok) {
                    hideTypingIndicator();
                    addMessage('ai', `Error: ${response.statusText}`);
                    return;
                }

                hideTypingIndicator();

                // Create a new AI message div for streaming content
                const aiMessageDiv = addMessage('ai', '');

                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let aiMessageContent = '';

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;

                    const chunk = decoder.decode(value);
                    const lines = chunk.split('\n').filter(line => line.trim() !== '');

                    for (const line of lines) {
                        if (line.startsWith('data: ')) {
                            const data = line.substring(6);
                            if (data === '[DONE]') continue;

                            try {
                                const parsed = JSON.parse(data);
                                if (parsed.choices && parsed.choices[0].delta && parsed.choices[0].delta.content) {
                                    aiMessageContent += parsed.choices[0].delta.content;
                                    aiMessageDiv.textContent = aiMessageContent;
                                    chatMessages.scrollTop = chatMessages.scrollHeight;
                                }
                            } catch (e) {
                                console.error('Error parsing JSON:', e);
                            }
                        }
                    }
                }
            });

            // Allow sending with Enter key (Shift+Enter for new line)
            chatInput.addEventListener('keydown', function(e) {
                if (e.key === 'Enter' && !e.shiftKey) {
                    e.preventDefault();
                    sendButton.click();
                }
            });
        });
    </script>
</body>
</html>